﻿using MXComponent.DAL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Visifire.Charts;

namespace MXComponent.Views.charview
{
    /// <summary>
    /// materialChar.xaml 的交互逻辑
    /// </summary>
    public partial class materialChar : Page
    {
        formuladetailhistoryDB fdhistoryDb = new formuladetailhistoryDB();
        public materialChar()
        {
            InitializeComponent();
        }
        private List<string> strListx = new List<string>() ;
        private List<string> strListy = new List<string>();
        private void BtnSearch_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(this.startDate.Text))
            {
                MessageBox.Show("开始日期不能为空！");
                this.startDate.Focus();
                return;
            }
            if (string.IsNullOrWhiteSpace(this.endDate.Text))
            {
                MessageBox.Show("结束日期不能为空！");
                this.endDate.Focus();
                return;
            }
            DataSet ds = fdhistoryDb.GetMaterialReport(this.startDate.Text, this.endDate.Text);
            this.dgvList.ItemsSource = ds.Tables[0].DefaultView;
            Simon.Children.Clear();
            strListx.Clear();
            strListy.Clear();

            foreach(DataRow dr in ds.Tables[0].Rows)
            {
                strListx.Add(dr["materialname"].ToString());
                strListy.Add(dr["totalnum"].ToString());
            }
            CreateChartColumn("物料统计", strListx, strListy);
        }

        #region 柱状图
        public void CreateChartColumn(string name, List<string> valuex, List<string> valuey)
        {
            //创建一个图标
            Chart chart = new Chart();

            //设置图标的宽度和高度
            chart.Width =800;
            chart.Height = 550;
            chart.Margin = new Thickness(30, 10, 10, 5);
            //是否启用打印和保持图片
            chart.ToolBarEnabled = false;

            //设置图标的属性
            chart.ScrollingEnabled = false;//是否启用或禁用滚动
            chart.View3D = true;//3D效果显示

            //创建一个标题的对象
            Title title = new Title();

            //设置标题的名称
            title.Text = Name;
            title.Padding = new Thickness(0, 10, 5, 0);

            //向图标添加标题
            chart.Titles.Add(title);

            Axis yAxis = new Axis();
            //设置图标中Y轴的最小值永远为0           
            yAxis.AxisMinimum = 0;
            //设置图表中Y轴的后缀          
            yAxis.Suffix = "g";
            chart.AxesY.Add(yAxis);

            // 创建一个新的数据线。               
            DataSeries dataSeries = new DataSeries();

            // 设置数据线的格式
            dataSeries.RenderAs = RenderAs.StackedColumn;//柱状Stacked


            // 设置数据点              
            DataPoint dataPoint;
            for (int i = 0; i < valuex.Count; i++)
            {
                // 创建一个数据点的实例。                   
                dataPoint = new DataPoint();
                // 设置X轴点                    
                dataPoint.AxisXLabel = valuex[i];
                //设置Y轴点                   
                dataPoint.YValue = double.Parse(valuey[i]);
                //添加一个点击事件        
                dataPoint.MouseLeftButtonDown += new MouseButtonEventHandler(dataPoint_MouseLeftButtonDown);
                //添加数据点                   
                dataSeries.DataPoints.Add(dataPoint);
            }

            // 添加数据线到数据序列。                
            chart.Series.Add(dataSeries);

            //将生产的图表增加到Grid，然后通过Grid添加到上层Grid.           
            Grid gr = new Grid();
            gr.Children.Add(chart);
            Simon.Children.Add(gr);
        }
        #endregion

        #region 点击事件
        //点击事件
        void dataPoint_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //DataPoint dp = sender as DataPoint;
            //MessageBox.Show(dp.YValue.ToString());
        }
        #endregion

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            this.startDate.Text = DateTime.Today.ToLongDateString();
            this.endDate.Text = DateTime.Today.ToLongDateString();
        }

        public static void Export(DataGrid dataGrid, string _fileName)
        {
            DataTable dt = new DataTable();
            for (int i = 0; i < dataGrid.Columns.Count; i++)
            {
                if (dataGrid.Columns[i].Visibility == System.Windows.Visibility.Visible)//只导出可见列
                {
                    dt.Columns.Add(dataGrid.Columns[i].Header.ToString());//构建表头

                }
            }

            for (int i = 0; i < dataGrid.Items.Count; i++)
            {
                int columnsIndex = 0;
                DataRow row = dt.NewRow();
                for (int j = 0; j < dataGrid.Columns.Count; j++)
                {
                    if (dataGrid.Columns[j].Visibility == System.Windows.Visibility.Visible)
                    {
                        if (columnsIndex == 0)
                        {
                            row[columnsIndex] = (i + 1).ToString();
                        }
                        else if (dataGrid.Items[i] != null && (dataGrid.Columns[j].GetCellContent(dataGrid.Items[i]) as TextBlock) != null)//填充可见列数据
                        {
                            row[columnsIndex] = (dataGrid.Columns[j].GetCellContent(dataGrid.Items[i]) as TextBlock).Text.ToString();
                        }
                        else
                        {
                            row[columnsIndex] = "";
                        }
                        columnsIndex++;
                    }
                }
                dt.Rows.Add(row);
            }


            System.Windows.Forms.SaveFileDialog save = new System.Windows.Forms.SaveFileDialog();
            //save.Filter = "Excel文件(*.xls)|";
            save.Title = "请选择要保存的路径";
            save.FileName = _fileName;

            if (save.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string fileName = save.FileName;
                //ExportFiles.ExportExcel(fileName, dt);
                ExcelHelper.DataTableToExcel(fileName, dt, "MySheet", true);
            }
        }
        private void btnExport_Click(object sender, RoutedEventArgs e)
        {
            Export(this.dgvList, "物料统计" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls");
        }
    }
}
